package com.jsmy.softapp.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.jsmy.softapp.R;
import com.jsmy.softapp.aroute.AR;
import com.jsmy.softapp.bean.PlanInfoBean;
import com.jsmy.softapp.net.API;
import com.jsmy.softapp.presenter.PlanInfoPresenter;
import com.jsmy.softapp.util.MesssageUtil;
import com.jsmy.softapp.util.ShareUtil;
import com.jsmy.softapp.util.ToastUtil;
import com.jsmy.softapp.view.PlanInfoView;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AR.A_PLANINFO)
public class PlanInfoActivity extends BaseActivity<PlanInfoView, PlanInfoPresenter> implements PlanInfoView, UMShareListener {
    @Autowired
    public String kid;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    PlanInfoBean.DataBean bean;

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_text)
    TextView tvText;
    @BindView(R.id.m_webview)
    WebView mWebview;

    String sharePt;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.action_setting1)
    TextView actionSetting1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_plan_info;
    }

    @Override
    protected PlanInfoPresenter createPresenter() {
        return new PlanInfoPresenter();
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlanInfoActivity.this.finish();
            }
        });
    }

    @Override
    protected void initData() {
        presenter.getgtjhinfo(kid);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showLoadingData(String type, Object object) {
        switch (type) {
            case API.APP_GTJH_INFO:
                bean = ((PlanInfoBean) object).getData();
                setPlanInfo();
                break;
            case API.APP_SAVE_RW_FLAG_INFO:
                ToastUtil.showShort(this, "操作成功！");
                PlanInfoActivity.this.finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        presenter.model.disposable.clear();
        super.onDestroy();
    }

    private void setPlanInfo() {
        tvName.setText("客户名称：" + bean.getXm());
        tvTitle.setText("计划标题：" + bean.getTitle());
        tvTime.setText("计划时间：" + bean.getSj());
        tvContent.setText("计划内容：" + bean.getNr());
        tvText.setText("" + bean.getUrl());
        if (bean.getIsok().equals("Y")){
            actionSetting1.setText("已完成");
        }
        mWebview.setVerticalScrollbarOverlay(true); //指定的垂直滚动条有叠加样式
        WebSettings settings = mWebview.getSettings();
        settings.setUseWideViewPort(true);//设定支持viewport
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);//设定支持缩放
        settings.setDomStorageEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mWebview.clearHistory();
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                return super.shouldOverrideUrlLoading(view, request);
            }

//            @Override
//            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                    handler.proceed();
////                super.onReceivedSslError(view, handler, error);
//            }

        });
        mWebview.loadUrl("" + bean.getUrl());
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA platform) {
        switch (platform) {
            case QQ:
                sharePt = "QQ";
                break;
            case QZONE:
                sharePt = "QZONE";
                break;
            case SINA:
                sharePt = "SINA";
                break;
            case WEIXIN:
                sharePt = "WEIXIN";
                break;
            case WEIXIN_CIRCLE:
                sharePt = "WEIXIN_CIRCLE";
                break;
            case SMS:
                sharePt = "SMS";
                break;
            case EMAIL:
                sharePt = "EMAIL";
                break;
        }
        presenter.savefxinfo("", bean.getRid(), sharePt, "");
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        ToastUtil.showShort(this, "分享失败，" + throwable.getMessage());
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        ToastUtil.showShort(this, "分享取消");
    }


    @OnClick({R.id.action_setting1, R.id.action_setting2, R.id.action_setting3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.action_setting1:
                if (bean.getIsok().equals("Y")){
                    ToastUtil.showShort(this,"计划已完成！");
                }else {
                    presenter.saverwflaginfo(kid, "Y");
                }

                break;
            case R.id.action_setting2:
                MesssageUtil.call(this, "12306");
                break;
            case R.id.action_setting3:
                ShareUtil.shareWeb(this, bean.getUrl(), bean.getTitle(), bean.getNr(), this);
                break;
        }
    }
}
